<?php

define (BTN_VIEW,1);
define (BTN_SIGNUP,2);
define (BTN_ADMIN,4);
define (BTN_DELETE,8);

class oh_game
{
	public $game_id=-1;
	public $venue="nowhere";
	public $system;
	public $scencode="INVALID";
	public $session_no=0;
	public $title="";
	public $subtitle="";
	public $minlevel=1;
	public $maxlevel=1;
	public $num_tables=0;
	public $cur_players=0;
	public $max_players=1;
	public $cur_gms=0;
	public $max_gms=1;

	public function render($buttons,$state)
	{
		if($state=="player")
		{
			echo "<tr>\n<td class=\"regtablebodyactive\">\n";
		}
		else if ($state=="GM")
		{
			echo "<tr>\n<td class=\"regtablebodygm\">\n";
		}
		else
		{
			if($this->cur_players>=$this->max_players)
				echo "<tr>\n<td class=\"regtablebodyfull\">\n";
			else
				echo "<tr>\n<td class=\"regtablebody\">\n";
		}
		echo "<span class=\"smalltext\">".$this->venue."</span><br />\n";
		echo "[".$this->system."] ".$this->title."<br />\n";
		if($this->subtitle != "")
		{
			echo "<div align=\"right\">".$this->subtitle."</div>\n";
		}
		
		if($this->session_no!=0)
		{
			echo "<span class=\"smalltext\">Session ".$this->session_no."</span><br />";
		}
	
		if ($this->minlevel==$this->maxlevel)
		{
			echo "<span class=\"smalltext\">Level ".$this->minlevel."</span><br />\n";
		}
		else
		{
			echo "<span class=\"smalltext\">Level ".$this->minlevel."-".$this->maxlevel."</span><br />\n";
		}

		echo $this->cur_gms." GMs";
		if ($this->cur_gms<$this->max_gms)
		{
			echo " (".($this->max_gms-$this->cur_gms)." needed)";
		}
		echo "<br />\n";
		
		echo $this->cur_players." players";
		if($this->cur_players<$this->max_players)
		{
			echo " (".($this->max_players-$this->cur_players)." openings)";
		}
		else if($this->cur_players>=$this->max_players)
		{
			echo " (waitlisting)";
		}
		
		echo "<br />\n";
		
		echo "<div align=\"right\" style=\"padding:0.2em\">";
		
		if($buttons & BTN_VIEW)
		{
			make_button("Details","gameinfo.php?game=".$this->game_id);
		}
		if($buttons & BTN_ADMIN)
		{
			make_button("Edit","gameinfo.php?admin=1&game=".$this->game_id);
		}
		if($buttons & BTN_DELETE)
		{
			make_button("Delete","gameinfo.php?action=kill&game=".$this->game_id);
		}
			
		echo "</td></tr>\n";
	}
}

function oh_game_update($game_id,$num_tables,$max_players,$max_gms)
{
	$game_id = filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	$num_tables = filter_var($num_tables, FILTER_SANITIZE_NUMBER_INT);
	$max_gms = filter_var($max_gms, FILTER_SANITIZE_NUMBER_INT);
	$max_players = filter_var($max_players, FILTER_SANITIZE_NUMBER_INT);
	
	$con=oh_sql_connect();
	
	$sql = "UPDATE `oh_games` SET 
		`num_tables` = '".$num_tables."',
		`max_gms` = '".$max_gms."',
		`max_players` = '".$max_players."'
		WHERE `game_id` = '".$game_id."'";
		
	oh_sql_query($con,$sql);
	
	return "success";
}

function game_show_info($game_id,$admin)
{
	$game_id = filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	$myrole = "none";
	
	$con=oh_sql_connect();

	$sql="SELECT `game_id`,`game_location`,`oh_games`.`scenariocode`,`scen_desc`,`session_no`,`num_tables`,`max_players`,`cur_players`,`max_gms`,`cur_gms`,`system`,`title`,`subtitle`,`levelmin`,`levelmax`
		FROM `oh_games` INNER JOIN `oh_scenarios` ON `oh_games`.`scenariocode`=`oh_scenarios`.`scenariocode` WHERE `game_id` = '.".$game_id.".'";

	
	$result=oh_sql_query($con,$sql);
	
	echo "<div style=\"display:inline-block;vertical-align:top;padding:0.5em;width:50%\">\n";
	if(($result->num_rows > 0) && $row=$result->fetch_assoc())	
	{
		echo "<b>".$row['system']."</b><br />\n";
		echo "<i>".$row['title']."</i><br />\n";
		if($row['subtitle']!="")
			echo "<i>".$row['subtitle']."</i><br />\n";
	
		if($row['levelmin']==$row['levelmax'])
			echo "For level ".$row['levelmin']."</i><br />\n";
		else
			echo "For levels ".$row['levelmin']." to ".$row['levelmax']."<br />\n";
			
		echo "<br />\n";

		$desc = str_replace("\n","<br \>\n",$row['scen_desc']);
		echo $desc;
		
		$max_gms=$row['max_gms'];
		$max_players=$row['max_players'];
		$num_tables=$row['num_tables'];
	}
	echo "</div>\n";
	
	mysqli_free_result($result);	
	
	$sql = "SELECT `record_id`,`game_id`,`user_id`,`firstname`,`lastname` FROM `oh_regrecords` 
		INNER JOIN `oh_users` ON `oh_regrecords`.`user_id` = `oh_users`.`uid` WHERE `is_gm`='1' AND `game_id`='".$game_id."'";
		
	$result=oh_sql_query($con,$sql);	
		
	echo "<div style=\"display:inline-block;vertical-align:top;padding:0.5em;\">\n";
	echo "<b>GMs:</b><br />\n";
	
	$i=1;
	
	if($result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())	
		{
			if($row['user_id']==$_SESSION['user_id'])
			{
				echo $i.": <span class=\"bluetext\">".$row['firstname']." ".$row['lastname']."</span><br />\n";
				$myrole="GM";
			}
			else
				echo $i.": ".$row['firstname']." ".$row['lastname']."<br />\n";
			$i++;
		}
	}
	if($i<=$max_gms)
	{
		while($i<=$max_gms)
		{
			echo $i.": - GM needed -";
			echo "<br />\n";
			$i++;
		}
		if(oh_is_logged_in() && $myrole=="none")
		{
			echo "<br />\n";
			make_button("Sign Up to GM","gmsignup.php?game=".$game_id);		
		}
	}
	
	if($myrole=="GM")
	{
		echo "<br />\n";
		make_button("Withdraw","leavegame.php?game=".$game_id);		
	}
			
	echo "</div>\n";
	echo "<div style=\"display:inline-block;vertical-align:top;padding:0.5em;%\">\n";

	$sql = "SELECT `record_id`,`game_id`,`user_id`,`firstname`,`lastname`,`role`,`level` FROM `oh_regrecords` 
		INNER JOIN `oh_users` ON `oh_regrecords`.`user_id` = `oh_users`.`uid` WHERE `is_gm`='0' AND `game_id`='".$game_id."'";
	
	$result=oh_sql_query($con,$sql);
	
	echo "<b>Players:</b><br />\n";
	$i=1;
	
	if($result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())	
		{
			if($row['user_id']==$_SESSION['user_id'])
			{
				echo $i.": <span class=\"bluetext\">".$row['firstname']." ".$row['lastname']." - ".$row['role']." ".$row['level']."</span><br />\n";
				$myrole="player";
			}
			else
				echo $i.": ".$row['firstname']." ".$row['lastname']." - ".$row['role']." ".$row['level']."<br />\n";
			$i++;	
		}
	}
	if($i<=$max_players)
	{

		while($i<=$max_players)
		{
			echo $i.": - open -<br />\n";
			$i++;
		}
	
		if(oh_is_logged_in() && $myrole=="none")
		{
			echo "<br />\n";
			make_button("Sign Up to Play","gamereg.php?game=".$game_id);		
		}
	}

	if($myrole=="player")
	{
		echo "<br />\n";
		make_button("Withdraw","leavegame.php?game=".$game_id);		
	}	
	
	echo "</div>\n";	
	
	mysqli_free_result($result);
	
	if($admin==1)
	{
		echo "<div style=\"display:inline-block;vertical-align:top;padding:0.5em;\">";
		echo "<form action=\"do_gameupdate.php\" method=\"POST\">\n";
		echo "<input type=\"hidden\" name=\"gameid\" value=\"".$game_id."\">";
		
		echo "Set new table count: ";
		echo "<input type=\"text\" name=\"tablecount\" size=\"3\" class=\"inputbox\" value=\"".$num_tables."\">";
		echo "<br />\n";
		
		echo "Set new max players: ";
		echo "<input type=\"text\" name=\"maxplayers\" size=\"3\" class=\"inputbox\" value=\"".$max_players."\">";
		echo "<br />\n";
		
		echo "Set new max GMs: ";
		echo "<input type=\"text\" name=\"maxgms\" size=\"3\" class=\"inputbox\" value=\"".$max_gms."\">";
		echo "<br />\n";
		
		echo "<input type=\"submit\" class=\"button\" value=\"Update Game\">";
		echo "</div>";
	}
	
	$con->close();
	
}

function oh_game_add($scenario,$slot,$location,$numtables,$numplayers,$numgms)
{
	$con=oh_sql_connect();

	$scenario=$con->real_escape_string($scenario);
	$location=$con->real_escape_string($location);
	$slot = filter_var($slot, FILTER_SANITIZE_NUMBER_INT);
	$numtables = filter_var($numtables, FILTER_SANITIZE_NUMBER_INT);
	$numplayers = filter_var($numplayers, FILTER_SANITIZE_NUMBER_INT);
	$numgms = filter_var($numgms, FILTER_SANITIZE_NUMBER_INT);
	
	$sql = "INSERT INTO `oh_games` (`game_location`,`scenariocode`,`timeslot`,`num_tables`,`max_players`,`max_gms`) VALUES
		('".$location."',
		'".$scenario."',
		'".$slot."',
		'".$numtables."',
		'".$numplayers."',
		'".$numgms."')";
		
	$result=oh_sql_query($con,$sql);
	
	return "success";

}

function oh_render_games_for_slot_logged_in($slot_id)
{
	$con=oh_sql_connect();

	$sql="SELECT `oh_games`.`game_id`,`game_location`,`oh_games`.`scenariocode`,`session_no`,`num_tables`,`max_players`,`cur_players`,`max_gms`,`cur_gms`,`system`,`title`,`subtitle`,`levelmin`,`levelmax`,`level`,`role`, `is_gm`
		FROM `oh_games` INNER JOIN `oh_scenarios` ON `oh_games`.`scenariocode`=`oh_scenarios`.`scenariocode` LEFT JOIN `oh_regrecords` ON `oh_regrecords`.`game_id` = `oh_games`.`game_id` AND `oh_regrecords`.`user_id` = '".$_SESSION['user_id']."'
		WHERE `oh_games`.`timeslot` = '".$slot_id."' ORDER BY `levelmax`";
		
	$result=oh_sql_query($con,$sql);
		
	if( $result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())
		{
			$game=new oh_game();
			$game->game_id=$row['game_id'];
			$game->scencode=$row['scenario_code'];
			$game->venue=$row['game_location'];
			$game->session_no=$row['session_no'];
			$game->title=$row['title'];
			$game->subtitle=$row['subtitle'];
			$game->minlevel=$row['levelmin'];
			$game->maxlevel=$row['levelmax'];
			$game->system=$row['system'];
			$game->num_tables=$row['num_tables'];
			$game->max_players=$row['max_players'];
			$game->max_gms=$row['max_gms'];
			$game->cur_players=$row['cur_players'];
			$game->cur_gms=$row['cur_gms'];
			
			if($row['is_gm']=="1")
				$game->render(BTN_VIEW,"GM");
			else if ($row['level']>0)
				$game->render(BTN_VIEW,"player");
			else 
				$game->render(BTN_VIEW,"default");
			unset($game);
		}
	} else {
		// do nothing
	}
}

function oh_render_games_for_slot_anon($slot_id)
{
	$con=oh_sql_connect();

	$sql="SELECT `game_id`,`game_location`,`oh_games`.`scenariocode`,`session_no`,`num_tables`,`max_players`,`cur_players`,`max_gms`,`cur_gms`,`system`,`title`,`subtitle`,`levelmin`,`levelmax`
		FROM `oh_games` INNER JOIN `oh_scenarios` ON `oh_games`.`scenariocode`=`oh_scenarios`.`scenariocode` WHERE `timeslot` = '".$slot_id."' ORDER BY `levelmax`";
		
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())
		{
			$game=new oh_game();
			$game->game_id=$row['game_id'];
			$game->scencode=$row['scenario_code'];
			$game->venue=$row['game_location'];
			$game->session_no=$row['session_no'];
			$game->title=$row['title'];
			$game->subtitle=$row['subtitle'];
			$game->minlevel=$row['levelmin'];
			$game->maxlevel=$row['levelmax'];
			$game->system=$row['system'];
			$game->num_tables=$row['num_tables'];
			$game->max_players=$row['max_players'];
			$game->max_gms=$row['max_gms'];
			$game->cur_players=$row['cur_players'];
			$game->cur_gms=$row['cur_gms'];
			$game->render(BTN_VIEW,"default");
			unset($game);
		}
	} else {
		// do nothing
	}
}

function oh_render_games_for_slot_admin($slot_id)
{
	$con=oh_sql_connect();

	$sql="SELECT `game_id`,`game_location`,`oh_games`.`scenariocode`,`session_no`,`num_tables`,`max_players`,`cur_players`,`max_gms`,`cur_gms`,`system`,`title`,`subtitle`,`levelmin`,`levelmax`
		FROM `oh_games` INNER JOIN `oh_scenarios` ON `oh_games`.`scenariocode`=`oh_scenarios`.`scenariocode` WHERE `timeslot` = '".$slot_id."' ORDER BY `levelmax`";
		
	$result=oh_sql_query($con,$sql);
	
	if( $result->num_rows > 0)
	{
		while($row=$result->fetch_assoc())
		{
			$game=new oh_game();
			$game->game_id=$row['game_id'];
			$game->scencode=$row['scenario_code'];
			$game->venue=$row['game_location'];
			$game->session_no=$row['session_no'];
			$game->title=$row['title'];
			$game->subtitle=$row['subtitle'];
			$game->minlevel=$row['levelmin'];
			$game->maxlevel=$row['levelmax'];
			$game->system=$row['system'];
			$game->num_tables=$row['num_tables'];
			$game->max_players=$row['max_players'];
			$game->max_gms=$row['max_gms'];
			$game->cur_players=$row['cur_players'];
			$game->cur_gms=$row['cur_gms'];
			$game->render(BTN_VIEW|BTN_ADMIN|BTN_DELETE,"default");
			unset($game);
		}
	} else {
		// do nothing
	}
}

function oh_render_games_for_slot($slot_id)
{
	$slot_id=filter_var($slot_id, FILTER_SANITIZE_NUMBER_INT);
	
	if(oh_is_logged_in())
		oh_render_games_for_slot_logged_in($slot_id);
	else
		oh_render_games_for_slot_anon($slot_id);
}

function oh_render_level_dropdown($game_id)
{

	$game_id=filter_var($game_id, FILTER_SANITIZE_NUMBER_INT);
	
	$sql="SELECT `game_id`,`levelmin`,`levelmax`
		FROM `oh_games` INNER JOIN `oh_scenarios` ON `oh_games`.`scenariocode`=`oh_scenarios`.`scenariocode` WHERE `game_id` = '.".$game_id.".'";

		$con=oh_sql_connect();

		
	$result=oh_sql_query($con,$sql);
	
	if(( $result->num_rows > 0) && ($row=$result->fetch_assoc()))
	{		
		echo "<select name=\"level\">\n";
	
		for($i=$row['levelmin'];$i<=$row['levelmax'];$i++)
		{
			echo "<option value=\"".$i."\">".$i."</option>\n";
		}

		echo "</select>\n";
	}
}
?>